;; Comprehensive test for call-with-current-continuation
(display "全面测试call/cc功能\n\n")

;; 测试1: 基本的continuation使用
(display "测试1: 基本的continuation使用\n")
(define result1
  (call/cc
   (lambda (k)
     (display "调用continuation前\n")
     (k 42)
     (display "这一行不会被执行\n"))))
(display "结果1: ")
(display result1)
(display "\n\n")

;; 测试2: 使用call/cc与if结合
(display "测试2: call/cc与if结合使用\n")
(define k-if #f)
(define x '(1 2 3))
(display "原始x的值: ")
(display x)
(display "\n")

(define result2
  (if (call/cc
       (lambda (k)
         (set! k-if k)
         (null? x)))
      '()
      (cdr x)))

(display "result2的值: ")
(display result2)
(display "\n")

(display "调用(k-if #t)...\n")
(define result3 (k-if #t))
(display "result3的值: ")
(display result3)
(display "\n")

(display "调用(k-if #f)...\n")
(define result4 (k-if #f))
(display "result4的值: ")
(display result4)
(display "\n\n")

;; 测试3: 从深层嵌套中返回
(display "测试3: 从深层嵌套中返回\n")
(define (deeply-nested k)
  (display "进入深层嵌套\n")
  (k "提前返回的值"))

(define result5
  (call/cc
   (lambda (exit)
     (display "外层函数\n")
     (deeply-nested exit)
     (display "这一行不会被执行\n"))))

(display "result5的值: ")
(display result5)
(display "\n\n")

;; 测试4: 多次调用同一个continuation
(display "测试4: 多次调用同一个continuation\n")
(define cont #f)
(define collected-values '())

(define (collect-value v)
  (set! collected-values (cons v collected-values)))

(call/cc
 (lambda (k)
   (set! cont k)
   (collect-value 1)))

(display "第一次调用后: ")
(display collected-values)
(display "\n")

(cont #f)

(display "第二次调用后: ")
(display collected-values)
(display "\n\n")

(display "所有测试完成！call/cc功能正常工作。\n")